package edu.hubu.controller.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import edu.hubu.model.CourseType;
import io.swagger.annotations.Api;
import net.moon.commons.EntityInfo;
import net.moon.commons.PageData;
import net.moon.commons.ResultsJson;
import net.moon.commons.StatusCode;
import net.moon.controller.CurdController;
import net.moon.jpa.service.DefaultService;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author moonlan
 * @date 2020/10/27 下午1:55
 */
@RestController
@RequestMapping("/course_type")
public class CourseTypeController extends CurdController<CourseType, String> {
    private final DefaultService<CourseType, String> service;
    private final ObjectMapper mapper;

    public CourseTypeController(DefaultService<CourseType, String> service, ObjectMapper mapper) {
        this.service = service;
        this.mapper = mapper;
    }

    @NotNull
    @Override
    protected CourseType register() {
        return CourseType.builder().build();
    }


    @PreAuthorize("hasAnyAuthority('findCourseType', 'find')")
    @NotNull
    @Override
    public ResultsJson findOneById(@NotNull @PathVariable("id") String id, @NotNull @RequestHeader String token) {
        CourseType entityById = service.findEntityById(id);
        return ResultsJson.toJson(StatusCode.SUCCESS, 1, 0, entityById);
    }

    @PreAuthorize("hasAnyAuthority('findCourseType', 'find')")
    @NotNull
    @Override
    public ResultsJson findByLikePage(@RequestParam(value = "currentPage", defaultValue = "0") int currentPage, @RequestParam(value = "size", defaultValue = "8") int size, @Nullable @RequestBody EntityInfo entityInfo, @NotNull @RequestHeader String token) {
        PageData<CourseType> paged = service.findEntitiesPaged(currentPage, size, entityInfo);
        return ResultsJson.toJson(StatusCode.SUCCESS, paged.getTotalSize(), paged.getCurrentPage(), paged.getData());
    }

    @PreAuthorize("hasAnyAuthority('findCourseType', 'find')")
    @NotNull
    @Override
    public ResultsJson findAllPage(@RequestParam(value = "currentPage", defaultValue = "0") int currentPage, @RequestParam(value = "size", defaultValue = "8") int size, @NotNull @RequestHeader String token) {
        PageData<CourseType> paged = service.findEntitiesPaged(currentPage, size);
        return ResultsJson.toJson(StatusCode.SUCCESS, paged.getTotalSize(), paged.getCurrentPage(), paged.getData());
    }

    @PreAuthorize("hasAnyAuthority('updateCourseType', 'update')")
    @NotNull
    @Override
    public ResultsJson updateById(@NotNull @PathVariable("id") String id, @NotNull @RequestBody CourseType t, @NotNull @RequestHeader String token) {
        CourseType entity = service.updateEntityById(id, t);
        return ResultsJson.toJson(StatusCode.SUCCESS, 1, 0, entity);
    }

    @PreAuthorize("hasAnyAuthority('deleteCourseType', 'delete')")
    @NotNull
    @Override
    public ResultsJson deleteById(@NotNull @PathVariable("id") String id, @NotNull @RequestHeader String token) {
        boolean entityById = service.logicDeleteEntityById(id);
        return ResultsJson.toJson(StatusCode.SUCCESS, 1, 0, entityById);
    }

    @PreAuthorize("hasAnyAuthority('forbidCourseType', 'forbid')")
    @NotNull
    @Override
    public ResultsJson forbiddenById(@NotNull @PathVariable("id") String id, @NotNull @RequestHeader String token) {
        boolean entityById = service.forbidEntityById(id);
        return ResultsJson.toJson(StatusCode.SUCCESS, 1, 0, entityById);
    }

    @PreAuthorize("hasAnyAuthority('addCourseType', 'add')")
    @NotNull
    @Override
    public ResultsJson add(@NotNull @RequestBody CourseType t, @NotNull @RequestHeader String token) {
        CourseType entity = service.addEntity(t);
        return ResultsJson.toJson(StatusCode.SUCCESS, 1, 0, entity);
    }

    @PreAuthorize("hasAnyAuthority('countCourseType', 'count')")
    @NotNull
    @Override
    public ResultsJson count(@NotNull @RequestHeader String token) {
        int count = service.getCount();
        return ResultsJson.toJson(StatusCode.SUCCESS, 1, 0, count);
    }
}
